export class TouchEvent {
    changeX: number
    changeY: number
    timer?: any
    movetimer?: any
    longTouchTime: number
    pageX: number[]
    pageY: number[]
    isLeft: string
    isUp: string
    constructor() {
        //longTouchTime:长摁延时
        //changeX:横向延迟
        //changeY:竖向延迟
        this.pageX = []
        this.pageY = []
        this.movetimer = false
        this.isLeft = 'UNKNOW'
        this.isUp = 'UNKNOW'
        this.changeX = 10
        this.changeY = 10
        this.longTouchTime = 500
        return new Proxy(this, {
            set: (target: TouchEvent, key: string, value: number[], receiver: TouchEvent) => {
                if (key === 'pageX') {
                    const oldval = target[key][0]
                    if (oldval > value[0] + target.changeX) {
                        this.isLeft = 'true'
                    } else if (oldval + target.changeX < value[0]) {
                        this.isLeft = 'false'
                    } else {
                        this.isLeft = 'UNKNOW'
                    }
                } else if (key === 'pageY') {
                    const oldval = target[key][0]
                    if (oldval > value[0] + target.changeY) {
                        this.isUp = 'true'
                    } else if (oldval + target.changeY < value[0]) {
                        this.isUp = 'false'
                    } else {
                        this.isUp = 'UNKNOW'
                    }
                }
                return Reflect.set(target, key, value, receiver)
            }
        })
    }
    setPages(event: any) {
        this.pageX = [...event.changedTouches].map((item: { pageX: any }) => item.pageX)
        this.pageY = [...event.changedTouches].map((item: { pageY: any }) => item.pageY)
    }
    fingers(count: number = 2) {
        //多指
        return this.pageX.length === count || this.pageX.length === count
    }
    longTouchCancal() {
        clearTimeout(this.timer)
    }
    longTouch(cb: any) {
        this.timer = setTimeout(() => {
            cb()
        }, this.longTouchTime);
    }
}